package com.shanghaionstar.discover.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.sqlite.entities.PoiSearchHistory;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.shanghaionstar.R;
import com.shanghaionstar.login.LoginActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;

import java.util.List;

public class NavPoiSearchHistoryAdapter extends CommonAdapter<PoiSearchHistory> {
	private BaseActivity context;
	private List<PoiSearchHistory> historyOrFavoriteInfoLists;
	private PoiSearchHistory poiSearchHistorys;
	private String flag;

	public NavPoiSearchHistoryAdapter(List<PoiSearchHistory> datas, BaseActivity context) {
		super(datas, context);
		this.context = context;
		this.historyOrFavoriteInfoLists = datas;
	}

	public NavPoiSearchHistoryAdapter(List<PoiSearchHistory> datas, Context context, String flag) {
		super(datas, context);
		this.historyOrFavoriteInfoLists = datas;
		this.flag = flag;
	}

	@Override
	public void getView(final int position, CommonViewHolder viewHolder) {
		ViewHolder holder = (ViewHolder) viewHolder;
		poiSearchHistorys = new PoiSearchHistory();
		poiSearchHistorys = historyOrFavoriteInfoLists.get(position);
		
		if (DeviceUtil.getLanguage().equalsIgnoreCase("en")){
			holder.btn_sendtbt.setTextSize(12f);
		}else{
			holder.btn_sendtbt.setTextSize(14f);
		}
		
		if ("gone".equals(flag)) {
			holder.btn_sendtbt.setVisibility(View.GONE);
			
			holder.text_vendor_name.setText(poiSearchHistorys.getName());
			holder.text_vendor_addres.setText(poiSearchHistorys.getAddress());
		} else {
			holder.text_vendor_name.setText(poiSearchHistorys.getName());
			holder.text_vendor_addres.setText(poiSearchHistorys.getAddress());

			holder.btn_sendtbt.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
//					BusinessUtil.sendReportByClickType(context, FunctionNameFindId.Searchpoi_history_navigation.getStringValue());
					PoiSearchHistory poiSearchHistory = historyOrFavoriteInfoLists.get(position);
					PoiItemInfo poiItemInfo = new PoiItemInfo();
					poiItemInfo.setTitle(poiSearchHistory.getName());
					poiItemInfo.setThreadId(poiSearchHistory.getSearchtime());
					poiItemInfo.setAddress(poiSearchHistory.getAddress());
					poiItemInfo.setLatitude(poiSearchHistory.getLat());
					poiItemInfo.setLongitude(poiSearchHistory.getLon());
					poiItemInfo.setCityName(poiSearchHistory.getCityname());
					poiItemInfo.setTelphone(poiSearchHistory.getPhone());
					String msgTitle = "";
					if(ApplicationUtil.getInstance().isLogin()){
//						NavSendTBTOrODDUtil.sendTbtOrOdd(context,poiItemInfo);
//						if (BusinessUtil.isServiceFeature(
//		                        FeatureEnum.SENDNAVDESTINATION.value(), context)) {
//		                	//个人中心设置TBT或者ODD的发送选项
//		                    if (PreferencesUtls.getInstance(context).getBoolean(
//		                            FinalConstants.SharedPreferencesConfig.TBT_CHOOSE_SAVE, true)) {
//		                    	msgTitle = context.getString(R.string.send_to_tbt_progress);
//		                        //音控导航   odd
//		        				NavSendTBTOrODDUtil navSendTBTOrODDUtil = new NavSendTBTOrODDUtil(context, context.getHandler(), poiItemInfo,msgTitle);
//		        				navSendTBTOrODDUtil.sendToTBTorODD(true);//true 音控  		false  车载
//		                    } else if (PreferencesUtls.getInstance(context).getBoolean(
//		                            FinalConstants.SharedPreferencesConfig.ODD_CHOOSE_SAVE, false)) {
//		                    	msgTitle = context.getString(R.string.send_to_odd_progress);
//		                        //车载导航 tbt
//		        				NavSendTBTOrODDUtil navSendTBTOrODDUtil = new NavSendTBTOrODDUtil(context, context.getHandler(), poiItemInfo,msgTitle);
//		        				navSendTBTOrODDUtil.sendToTBTorODD(false);//true 音控      	false  车载
//		                    }
//		                }else {
//		                	msgTitle = context.getString(R.string.send_to_tbt_progress);
//		                	 //音控导航   odd
//		    				NavSendTBTOrODDUtil navSendTBTOrODDUtil = new NavSendTBTOrODDUtil(context, context.getHandler(), poiItemInfo,msgTitle);
//		    				navSendTBTOrODDUtil.sendToTBTorODD(true);//true 音控  		false  车载
//		                }
					}else if (ApplicationUtil.getInstance().isAutoLogings()) {
						AlertDialogUtil.showAlertDialog(context, context.getString(R.string.dialog_warning),
								context.getString(R.string.loading));
					}else{
						Intent intent2 = new Intent(context, LoginActivity.class);
						context.startActivity(intent2);
					}
				}
			});
		}
	}
	

	@Override
	public CommonViewHolder getViewHolder() {
		return new ViewHolder();
	}

	@Override
	public int layoutId() {
		return R.layout.nav_history_fav_listview_item;
	}

	private class ViewHolder extends CommonViewHolder {
		@FindViewById(R.id.text_vendor_name)
		TextView text_vendor_name;
		@FindViewById(R.id.text_vendor_addres)
		TextView text_vendor_addres;
		@FindViewById(R.id.btn_sendtbt)
		RippleCustomBtn btn_sendtbt;
	}

}
